class TwitterProfile < ActiveRecord::Base

  belongs_to :user

  validates :uid, :presence => true,
                  :uniqueness => true

  def self.create_with_omniauth( auth )
    create! do |user|
      user.uid = auth["uid"]
      user.access_token = auth["credentials"]["token"]
      user.access_token_secret = auth["credentials"]["secret"]
      user.name = auth["info"]["name"]
      user.screen_name = auth["info"]["nickname"]
      user.location = auth["info"]["location"]
      user.image = auth["info"]["image"]
      user.description = auth["info"]["description"]
      user.website = auth["info"]["urls"]["Website"]
      user.follows_cuxibamba = Twitter.friendship( auth["info"]["nickname"], 'cuxibamba' ).target.followed_by
    end
  end

  def update_with_omniauth( auth )
    self.access_token = auth["credentials"]["token"]
    self.access_token_secret = auth["credentials"]["secret"]
    self.name = auth["info"]["name"]
    self.screen_name = auth["info"]["nickname"]
    self.location = auth["info"]["location"]
    self.image = auth["info"]["image"]
    self.description = auth["info"]["description"]
    self.website = auth["info"]["urls"]["Website"]
    self.follows_cuxibamba = Twitter.friendship( auth["info"]["nickname"], 'cuxibamba' ).target.followed_by
    save!
  end

end
